package cn.lili.event.impl;

import cn.lili.event.OrderStatusChangeEvent;
import cn.lili.event.TradeEvent;
import cn.lili.modules.message.client.WechatMessageClient;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.order.entity.dto.OrderMessage;
import cn.lili.modules.order.order.entity.vo.OrderVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信消息执行器
 *
 * @author Chopper
 * @version v1.0 2021-04-19 14:25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WechatMessageExecute implements OrderStatusChangeEvent, TradeEvent {

    private final WechatMessageClient wechatMessageClient;

    @Override
    public void tradeCreate(TradeDTO tradeDTO) {
        for (OrderVO orderVO : tradeDTO.getOrderVO()) {
            try {
                wechatMessageClient.sendWechatMessage(orderVO.getSn());
            } catch (Exception e) {
                log.warn("微信消息发送失败：" + orderVO.getSn(), e);
            }
        }
    }

    @Override
    public void orderChange(OrderMessage orderMessage) {

      switch (orderMessage.getNewStatus()) {
        case PAID, UNDELIVERED, DELIVERED, COMPLETED -> {
          try {
            wechatMessageClient.sendWechatMessage(orderMessage.getOrderSn());
          } catch (Exception e) {
            log.warn("微信消息发送失败", e);
          }
        }
        default -> {
        }
      }
    }
}
